/**
 * @file hdu/4825/main
 * @brief 给定 N 个正整数的集合，之后发起 M 次询问，每次询问包含一个
 * 正整数 S，请从集合中找出一个正整数 K，使得 xor(K, S) 最大
 *
 * 构建一个 01-Trie 树，每次选与当前位不同的边（使 xor 结果为 1）
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/6/18 21:56:30
 **/

#include <iostream>
#include <vector>
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 10, WL = 32;
struct Trie01 {
  int sz, ch[WL * N][2];
  ll val[WL * N];
  int newNode() {
    ch[sz][0] = ch[sz][1] = 0, val[sz] = 0;
    return sz++;
  }
  void init() {
    sz = 0;
    newNode();
  }
  void insert(ll x) {
    int u = 0;
    for (int i = WL; i >= 0; --i) {
      int v = (x >> i) & 1, &c = ch[u][v];
      if (!c) c = newNode();
      u = c;
    }
    val[u] = x;
  }
  ll query(ll x) {
    int u = 0;
    for (int i = WL; i >= 0; --i) {
      int v = (x >> i) & 1;
      u = (ch[u][v ^ 1] ? ch[u][v ^ 1] : ch[u][v]);
    }
    return val[u];
  }
};
Trie01 trie;
int main() {
  // High rating and good luck!
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  for (int n, m, kase = 1; cin >> n >> m && kase <= t; ++kase) {
    ll x;
    trie.init();
    while (n--) cin >> x, trie.insert(x);
    printf("Case #%d:\n", kase);
    while (m--) cin >> x, printf("%lld\n", trie.query(x));
  }
  return 0;
}
